//
//  CheckVersion.m
//  BusinessMarket
//
//  Created by Michael Li on 2019/1/4.
//  Copyright © 2019年 Michael Li. All rights reserved.
//

#import "CheckVersion.h"
@interface CheckVersion()
//@property(nonatomic,strong) NSString *versionAPPStore;
//@property(nonatomic,strong) NSString *versionLocal;
@end
@implementation CheckVersion
+ (void)POSTWithURLIndex:(int )index
              parameters:(id)parameters
                 success:(SuccessBlock)successBlock
                 failure:(FailureBlock)failureBlock
{
    NSArray *tempURL = @[@"/version/check"
                         ];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *urlString = [NSString stringWithFormat:@"%@%@",kUrl,tempURL[index]];
    [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (successBlock) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            successBlock(dic);
            NSInteger integer = [[dic objectForKey:@"status"] intValue] ;
            if (integer == 200) {
                switch (index) {
                    case 0:
                    {
//                        NSNotification *notification = [NSNotification notificationWithName:@"checkVersion" object:nil userInfo:dic];
//                        [[NSNotificationCenter defaultCenter]postNotification:notification];
                    }
                        break;
                    default:
                        break;
                }
                
            }else {
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
            XLog(@"网络异常 - T_T%@", error);

        }
        
    }];
}
//获取当前版本
+(NSString *)getAPPInfo{
    NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}
////获取App Store版本
+(NSString *)getAPPStoreInfo{
    //最新的获取数据的地址：
    NSString *appID=@"1441941518";
    NSString *urlStr=[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",appID];
    NSURL *url=[NSURL URLWithString:urlStr];
    NSData *json = [NSData dataWithContentsOfURL:url];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];//解析json文件
    NSArray *results = [dict objectForKey:@"results"];
    NSDictionary *dictSub = results[0];
//    self.versionAPPStore = [dictSub objectForKey:@"releaseNotes"];
//    self.versionAPPStore = [dictSub objectForKey:@"version"];
    //XLog(@"self.version:%@",self.version);
    //XLog(@"versionNotes1:%@",self.versionNotes);
    NSArray *resultsSub = [dict objectForKey:@"results"];
    NSDictionary *result = [resultsSub objectAtIndex:0];
    NSString *versionStr = [result objectForKey:@"version"];//获得AppStore中的app的版本
    return versionStr;
}
@end
